﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1.DTO;
using WindowsFormsApplication1.DAO;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
    public partial class BienBanCoiThi : Form
    {
        public BienBanCoiThi()
        {
            InitializeComponent();
        }

        private void BienBanCoiThi_Load(object sender, EventArgs e)
        {
            LoadHocPhan();
            LoadLop();
            LoadNamHoc();
            LoadHocKy();
            LoadLanThi();
            LoadHeDaoTao();
            LoadPhong();
            LoadGiaoVien();
            txtThoiGian.Text = "90";
            txtSoBai1.Text = "30";
            txtSoBai2.Text = "30";
            bInsertGeneralInfo = false;
            s1[0] = "0";
        }

        private void LoadPhong()
        {
            List<PhongDTO> ds = PhongDAO.GetList();
            cbPhongThi.Text = ds[0].tenPhong;
            foreach (PhongDTO phong in ds)
                cbPhongThi.Items.Add(phong);
            cbPhongThi.SelectedIndex = 0;
        }

        private void LoadGiaoVien()
        {
            List<GiaoVienDTO> ds = GiaoVienDAO.GetList();
            foreach (GiaoVienDTO gv in ds)
            {
                cbGiamThi1.Items.Add(gv);
                cbGiamThi2.Items.Add(gv);
            }
            if (ds.Count >= 2)
            {
                cbGiamThi1.SelectedIndex = 0;
                cbGiamThi2.SelectedIndex = 1;
            }
        }

        private void LoadLanThi()
        {
            List<LanThiDTO> ds = LanThiDAO.GetList();
            cbLanThi.Text = ds[0].tenLanThi;
            foreach (LanThiDTO lt in ds)
                cbLanThi.Items.Add(lt);
            cbLanThi.SelectedIndex = 0;
        }

        private void LoadHeDaoTao()
        {
            List<HeDaoTaoDTO> ds = HeDaoTaoDAO.GetList();
            cbHeDaoTao.Text = ds[0].name;
            foreach (HeDaoTaoDTO hdt in ds)
                cbHeDaoTao.Items.Add(hdt);
            cbHeDaoTao.SelectedIndex = 0;
        }

        private void LoadNamHoc()
        {
            List<NamHocDTO> ds = NamHocDAO.GetList();
            cbNamHoc.Text = ds[0].value;
            foreach (NamHocDTO nam in ds)
                cbNamHoc.Items.Add(nam);
            cbNamHoc.SelectedIndex = 0;
        }

        private void LoadLop()
        {
            List<LopDTO> ds = LopDAO.GetList();
            foreach (LopDTO lop in ds)
                cbLop.Items.Add(lop);
            cbLop.SelectedIndex = 0;
        }

        private void LoadHocPhan()
        {
            List<HocPhanDTO> ds = HocPhanDAO.GetList();
            foreach (HocPhanDTO hp in ds)
                cbHocPhan.Items.Add(hp);
            cbHocPhan.SelectedIndex = 0;
        }

        private void LoadHocKy()
        {
            List<HocKyDTO> ds = HocKyDAO.GetList();
            cbHocKy.Text = ds[0].value;
            foreach (HocKyDTO hk in ds)
                cbHocKy.Items.Add(hk);
            cbHocKy.SelectedIndex = 1;
        }
        private string[] s1 = new string[4];
        private string[] s2 = new string[4];
        private bool bInsertGeneralInfo=false;
        private void btEnterDetail_Click(object sender, EventArgs e)
        {
            if (!bInsertGeneralInfo)
            {
                if (Insert_DataTo_BienBanCoiThi())
                {
                    Insert_Data_To_ChiTietCoiThi();
                }
            }
        }

        

        private bool Insert_Data_To_ChiTietCoiThi()
        {
            int maxID = BienBanCoiThiDAO.getMaxID();
            ChiTietCoiThiDTO ctct = new ChiTietCoiThiDTO();
            ctct.id_bienban = maxID;
            if (cbGiamThi1.Text != "")
                ctct.id_giaovien1 = ((GiaoVienDTO)cbGiamThi1.SelectedItem).maGiaoVien;
            if (txtSoBai1.Text != "")
                ctct.soBaiThu1 = int.Parse(txtSoBai1.Text.Trim());
            if (cbGiamThi2.Text != "")
                ctct.id_giaovien2 = ((GiaoVienDTO)cbGiamThi2.SelectedItem).maGiaoVien;
            if (txtSoBai2.Text != "")
                ctct.soBaiThu2 = int.Parse(txtSoBai2.Text.Trim());
            s1[2] = cbGiamThi1.Text.Trim();
            s1[3] = txtSoBai1.Text.Trim();
            s2[2] = cbGiamThi2.Text.Trim();
            s2[3] = txtSoBai2.Text.Trim();
            if (ChiTietCoiThiDAO.Insert(ctct))
            {
                s1[0] = (int.Parse(s1[0]) + 1).ToString();
                addDataToGridView(s1);
                addDataToGridView(s2);
                return true;
            }
            return false;
        }

        private void addDataToGridView(string[] s)
        {
            if (s[3] == "")
                s[3] = "0";
            if (s[2] != "")
                dgDetails.Rows.Add(s);
        }

        private bool Insert_DataTo_BienBanCoiThi()
        {
            BienBanCoiThiDTO bbct = new BienBanCoiThiDTO();
            bbct.lop = ((LopDTO)cbLop.SelectedItem).maLop;
            bbct.hocPhan = ((HocPhanDTO)cbHocPhan.SelectedItem).maHocPhan;
            bbct.thoiGian = tryParseInteger(txtThoiGian.Text.Trim());
            bbct.ngayThi = dateTimePicker1.Value;
            bbct.lanThi = ((LanThiDTO)cbLanThi.SelectedItem).id;
            bbct.namHoc = ((NamHocDTO)cbNamHoc.SelectedItem).id;
            bbct.hocKy = ((HocKyDTO)cbHocKy.SelectedItem).id;
            bbct.phong = ((PhongDTO)cbPhongThi.SelectedItem).maPhong;
            s1[1] = cbPhongThi.Text.Trim();
            bbct.heDaoTao = ((HeDaoTaoDTO)cbHeDaoTao.SelectedItem).id;

            if (BienBanCoiThiDAO.Insert(bbct))
                return true;
            return false;
        }

        private int tryParseInteger(string str)
        {
            try
            {
                return int.Parse(str);
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
